package com.example.clock.timeAnalysisPage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clock.Export.ExportData;
import com.example.clock.R;
import com.example.clock.basicClass.HelpFunc;
import com.example.clock.controlClass.DatabaseHelper;

import java.io.File;

public class TimeAnalysisPage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_analysis_page);
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        Context ct = this;

        Button button1 = (Button) findViewById(R.id.button18);
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast toast = new Toast(TimeAnalysisPage.this);
                String s = ExportData.exportAllTaskToCSV(db,ct);
                toast.setText(s);
                toast.show();
            }
        });

    }
    public void export1(View v){
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        Context ct = TimeAnalysisPage.this;
        Toast toast = new Toast(TimeAnalysisPage.this);
        String s = ExportData.exportAllTaskToCSV(db,ct);
        toast.setText(s);
        toast.show();
        TextView t = findViewById(R.id.textView53);
        t.setText(s);
    };
    public void export12(View v){
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        Context ct = TimeAnalysisPage.this;
        Toast toast = new Toast(TimeAnalysisPage.this);
        String s = ExportData.convertCsvToExcel(ExportData.exportAllTaskToCSV(db,ct));
        toast.setText(s);
        toast.show();
        TextView t = findViewById(R.id.textView53);
        t.setText(s);
    };
    public void exportPlan(View v){
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        Context ct = TimeAnalysisPage.this;
        Toast toast = new Toast(TimeAnalysisPage.this);
        String s = ExportData.exportAllPlanToCSV(db,ct);
        toast.setText(s);
        toast.show();
        TextView t = findViewById(R.id.textView53);
        t.setText(s);
    }
    public void exportPlanToXLSX(View v){
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        Context ct = TimeAnalysisPage.this;
        Toast toast = new Toast(TimeAnalysisPage.this);
        String s = ExportData.convertCsvToExcel(ExportData.exportAllPlanToCSV(db,ct));
        toast.setText(s);
        toast.show();
        TextView t = findViewById(R.id.textView53);
        t.setText(s);
    }
    public void exportClockedTask(View v){
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        Context ct = TimeAnalysisPage.this;
        Toast toast = new Toast(TimeAnalysisPage.this);
        String s = ExportData.exportClockedTaskToCSV(db,ct);
        toast.setText(s);
        toast.show();
        TextView t = findViewById(R.id.textView53);
        t.setText(s);
    }
    public void exportClockedTaskToXLSX(View v){
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        Context ct = TimeAnalysisPage.this;
        Toast toast = new Toast(TimeAnalysisPage.this);
        String s = ExportData.convertCsvToExcel(ExportData.exportClockedTaskToCSV(db,ct));
        toast.setText(s);
        toast.show();
        TextView t = findViewById(R.id.textView53);
        t.setText(s);
    }
    public void exportTaskByDate(View v){
        //获取日期
        EditText vi = (EditText) findViewById(R.id.editxx);
        String date = vi.getText().toString();
        if(HelpFunc.checkForDateFormat(TimeAnalysisPage.this,date) && HelpFunc.checkForNullFormat(TimeAnalysisPage.this,date)){
            //存入文件
            SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
            Context ct = TimeAnalysisPage.this;
            Toast toast = new Toast(TimeAnalysisPage.this);
            String s = ExportData.exportTaskToCSVByDate(db,ct,date);
            toast.setText(s);
            toast.show();
            TextView t = findViewById(R.id.textView53);
            t.setText(s);
        }
    }
    public void exportTaskByDateToXLSX(View v){
        //获取日期
        EditText vi = (EditText) findViewById(R.id.editxx);
        String date = vi.getText().toString();
        if(HelpFunc.checkForDateFormat(TimeAnalysisPage.this,date) && HelpFunc.checkForNullFormat(TimeAnalysisPage.this,date)){
            //存入文件
            SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
            Context ct = TimeAnalysisPage.this;
            Toast toast = new Toast(TimeAnalysisPage.this);
            String s = ExportData.exportTaskToCSVByDate(db,ct,date);
            toast.setText(s);
            toast.show();
            TextView t = findViewById(R.id.textView53);
            t.setText(s);
        }
    }

}